
/*
 * (a)  (C) 1990 by Adobe Systems Incorporated. All rights reserved.
 *
 * (b)  If this Sample Code is distributed as part of the Display PostScript
 *	System Software Development Kit from Adobe Systems Incorporated,
 *	then this copy is designated as Development Software and its use is
 *	subject to the terms of the License Agreement attached to such Kit.
 *
 * (c)  If this Sample Code is distributed independently, then the following
 *	terms apply:
 *
 * (d)  This file may be freely copied and redistributed as long as:
 *	1) Parts (a), (d), (e) and (f) continue to be included in the file,
 *	2) If the file has been modified in any way, a notice of such
 *      modification is conspicuously indicated.
 *
 * (e)  PostScript, Display PostScript, and Adobe are registered trademarks of
 *	Adobe Systems Incorporated.
 * 
 * (f) THE INFORMATION BELOW IS FURNISHED AS IS, IS SUBJECT TO
 *	CHANGE WITHOUT NOTICE, AND SHOULD NOT BE CONSTRUED
 *	AS A COMMITMENT BY ADOBE SYSTEMS INCORPORATED.
 *	ADOBE SYSTEMS INCORPORATED ASSUMES NO RESPONSIBILITY
 *	OR LIABILITY FOR ANY ERRORS OR INACCURACIES, MAKES NO
 *	WARRANTY OF ANY KIND (EXPRESS, IMPLIED OR STATUTORY)
 *	WITH RESPECT TO THIS INFORMATION, AND EXPRESSLY
 *	DISCLAIMS ANY AND ALL WARRANTIES OF MERCHANTABILITY, 
 *	FITNESS FOR PARTICULAR PURPOSES AND NONINFRINGEMENT
 *	OF THIRD PARTY RIGHTS.
 */

/*
 * 	Document.m
 *
 *	Portions of the source code in this file are based on source code
 *	from the Draw example application provided by NeXT.
 *
 *	The Document class serves to keep track of the global information
 *	concerning a particular file. It sets up the window and the view.
 *
 *	Version:	2.0
 *	Author:	Ken Fromm
 *	History:
 *			03-07-91		Added this comment.
 */

#import "Document.h"
#import "ImportApp.h"
#import "DrawingView.h"
#import "SaveAsPanel.h"

#import <appkit/Matrix.h>
#import <appkit/OpenPanel.h>
#import <appkit/nextstd.h>
#import <appkit/publicWraps.h>
#import <objc/hashtable.h>
#import <string.h>

const NXRect			DefaultWindowRect = {300.0, 110.0, 575.0, 660.0};

@implementation Document

/*
 *	Creates a new, empty, document.
 */
+ new
{
	self = [super new];

	[self  createWindow];
	[self  setWindow];

	return self;
}

- setWindow
{
	[windowId  makeFirstResponder:[windowId  contentView]];
	[windowId  setDelegate:self];
	[windowId  setTitle:"Epsf Introduction"];
	[windowId  display];

	[windowId  makeKeyAndOrderFront:self];

	return self;
}

/*
*	Create the drawing window and place a scrollview as the content view.
*	A DrawingView instance is placed as the DocView of the ScrollView.
*/
- createWindow
{
	id				drawingView;

	drawingView = [DrawingView  newFrame:&DefaultWindowRect];
	windowId = [Window newContent:&DefaultWindowRect
			style:NX_TITLEDSTYLE
			backing:NX_BUFFERED
			buttonMask:0
			defer:NO];
	[[windowId  setContentView:drawingView]  free];

	return self;
}

- window
{
	return windowId;
}

- (char const *) filename
{
	return [[SaveAsPanel  new]  filename];
}

/*
*	Bring up the import (open) panel to obtain the file and then pass
*	to the drawing view.
*/
- import:sender
{
	id						openpanel;

	static const char *const		filetype[4] = {"ps", "eps", "tiff", NULL};

	openpanel = [OpenPanel  new];
	if ([openpanel runModalForTypes:filetype])
		[[windowId  contentView]  importFile:[openpanel  filename]];

	return self;
}

- saveTo:sender
{
	id			savepanel;

	NXStream	*stream;

	savepanel = [[SaveAsPanel  new]  setSaveTo];
	if ([savepanel	runModal])
	{	
		stream = NXOpenMemory(NULL, 0, NX_WRITEONLY);
		if (stream)
		{
			[[windowId  contentView]  writePSToStream:stream];
			NXSaveToFile(stream, [savepanel  filename]);
			NXCloseMemory(stream, NX_FREEBUFFER);
		}
		else
			Notify("Save Error", "Cannot open a stream to the file.");
	}

	return self;
}

@end

